unit u_Planocontas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FMTBcd, Grids, DBGrids, JvExDBGrids, JvDBGrid, DB, DBClient,
  Provider, SqlExpr, JvExControls, JvLabel, ExtCtrls, MMJPanel, DBCtrls,
  Mask, StdCtrls, Buttons;

type
  Tf_Planocontas = class(TForm)
    MMJPanel2: TMMJPanel;
    lbl14: TLabel;
    lbl15: TLabel;
    JvLabel1: TJvLabel;
    sds_desp: TSQLDataSet;
    dsp_desp: TDataSetProvider;
    ds_desp: TClientDataSet;
    ds_despCODIGO: TIntegerField;
    ds_despNOME: TStringField;
    ds_despCONTA: TStringField;
    dsDtSrc: TDataSource;
    MMJPanel1: TMMJPanel;
    btnGravar: TBitBtn;
    btnIncluir: TBitBtn;
    btnCancelar: TBitBtn;
    btnExcluir: TBitBtn;
    btnSair: TBitBtn;
    pnl1: TPanel;
    pnl2: TPanel;
    lbl2: TLabel;
    lbl3: TLabel;
    lbl4: TLabel;
    lbl5: TLabel;
    lbl6: TLabel;
    lbl7: TLabel;
    lbl8: TLabel;
    lbl9: TLabel;
    lbl10: TLabel;
    lbl11: TLabel;
    lbl12: TLabel;
    lbl13: TLabel;
    lbl1: TLabel;
    edt1: TEdit;
    edt2: TEdit;
    cbb1: TComboBox;
    btn1: TBitBtn;
    btn2: TBitBtn;
    btn3: TBitBtn;
    cbb2: TComboBox;
    btn4: TBitBtn;
    btn5: TBitBtn;
    cbb3: TComboBox;
    btn6: TBitBtn;
    btn7: TBitBtn;
    dbedtCONTA: TDBEdit;
    dbedtCONTAPAI: TDBEdit;
    dbedtCODREDUZIDO: TDBEdit;
    dbedtNOME: TDBEdit;
    dbedtDESCRICAO: TDBEdit;
    dbcbbCONSOLIDA: TDBComboBox;
    dbcbbRATEIO1: TDBComboBox;
    dbcbbRATEIO: TDBComboBox;
    JvDBGrid1: TJvDBGrid;
    buscaFamilia: TSQLDataSet;
    buscaFamiliaID_FAMILIA: TIntegerField;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btn6Click(Sender: TObject);
    procedure btn7Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnIncluirClick(Sender: TObject);
    procedure dsDtSrcStateChange(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure btnCancelarClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure btnSairClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  f_Planocontas: Tf_Planocontas;

implementation

uses UDM, uCategoria, uFamilia, sCtrlResize;

{$R *.dfm}

procedure Tf_Planocontas.btn1Click(Sender: TObject);
var
  str_sql2: string;
begin
  if DM.ds_1_planoc.Active then
    DM.ds_1_planoc.Close;
  if edt1.Text <> '' then
  begin
    str_sql2 := 'Select * from PLANO_CONTA ';
    str_sql2 := str_sql2 + 'WHERE CONTA LIKE ';
    str_sql2 := str_sql2 + '''' + edt1.Text + '%''';
    str_sql2 := str_sql2 + ' order by CONTA';
    DM.ds_1_planoc.CommandText := str_sql2;
    DM.ds_1_planoc.Open;
  end
  else
  begin
    str_sql2 := 'Select * from PLANO_CONTA ';
    str_sql2 := str_sql2 + ' order by CONTA';
    DM.ds_1_planoc.CommandText := str_sql2;
    DM.ds_1_planoc.Open;
  end;
end;

procedure Tf_Planocontas.btn2Click(Sender: TObject);
Var str_sql2: string;
begin
  inherited;
  if DM.ds_1_planoc.Active then
    DM.ds_1_planoc.Close;
  if Edt2.Text <> '' then
  begin
    str_sql2 := 'Select * from PLANO_CONTA ';
    str_sql2 := str_sql2 + 'WHERE CODREDUZIDO = ';
    str_sql2 := str_sql2 + '''' + edt2.Text + '''';
    str_sql2 := str_sql2 + ' order by CONTA';
    DM.ds_1_planoc.CommandText := str_sql2;
    DM.ds_1_planoc.Open;
  end
  else
  begin
    str_sql2 := 'Select * from PLANO_CONTA ';
    str_sql2 := str_sql2 + ' order by CONTA';
    DM.ds_1_planoc.CommandText := str_sql2;
    DM.ds_1_planoc.Open;
  end;
end;

procedure Tf_Planocontas.btn3Click(Sender: TObject);
 Var str_sql2: string;
begin
  if ds_desp.Active then
    ds_desp.Close;
  ds_desp.Params[0].AsString := cbb1.Text;
  ds_desp.Open;
  if DM.ds_1_planoc.Active then
    DM.ds_1_planoc.Close;
  if cbb1.Text <> '' then
  begin
    str_sql2 := 'Select * from PLANO_CONTA ';
    str_sql2 := str_sql2 + 'WHERE CONTAPAI = ';
    str_sql2 := str_sql2 + '''' + ds_despCONTA.AsString + '''';
    str_sql2 := str_sql2 + ' order by CONTA';
    DM.ds_1_planoc.CommandText := str_sql2;
    DM.ds_1_planoc.Open;
  end
  else
  begin
    str_sql2 := 'Select * from PLANO_CONTA ';
    str_sql2 := str_sql2 + ' order by CONTA';
    DM.ds_1_planoc.CommandText := str_sql2;
    DM.ds_1_planoc.Open;
  end;
end;

procedure Tf_Planocontas.btn4Click(Sender: TObject);
begin
  fFamilia := TfFamilia.Create(Application);
  try
    dm.v_Origem := 'planocontas';
    fFamilia.ShowModal;
  finally
    cbb2.Text := fFamilia.cds_grupoDESCRICAO.AsString;
    fFamilia.Free;
    cbb2.SetFocus;
  end;
end;

procedure Tf_Planocontas.btn5Click(Sender: TObject);
 Var str_sql: String;
begin
  inherited;
  if ds_desp.Active then
    ds_desp.Close;
  ds_desp.Params[0].AsString := cbb2.Text;
  ds_desp.Open;
  if DM.ds_1_planoc.Active then
    DM.ds_1_planoc.Close;
  if cbb2.Text <> '' then
  begin
    str_sql := 'Select * from PLANO_CONTA ';
    str_sql := str_sql + 'WHERE CONTAPAI = ';
    str_sql := str_sql + '''' + ds_despCONTA.AsString + '''';
    DM.ds_1_planoc.CommandText := str_sql;
    DM.ds_1_planoc.Open;
  end
  else
  begin
    str_sql := 'Select * from PLANO_CONTA ';
    DM.ds_1_planoc.CommandText := str_sql;
    DM.ds_1_planoc.Open;
  end;
end;

procedure Tf_Planocontas.btn6Click(Sender: TObject);
begin
  if (cbb2.Text = '') then
  begin
    MessageDlg('Antes de cadastrar o SubGrupo primeiro'+#13+#10+'         cadastre ou escolha um Grupo', mtWarning, [mbOK], 0);
    Exit;
    cbb2.SetFocus;
  end
  else
  begin
    if (buscaFamilia.Active) then
      buscaFamilia.Close;
    buscaFamilia.Params[0].AsString := cbb2.Text;
    buscaFamilia.Open;
    if (not buscaFamilia.IsEmpty) then
       dm.v_IDFAMILIA := buscaFamiliaID_FAMILIA.AsInteger
    else
       dm.v_IDFAMILIA := 0;
  end;
  if (dm.v_IDFAMILIA = 0) then
  begin
     MessageDlg('Grupo Invalido', mtError, [mbOK], 0);
     Exit;
  end;
  fCategoria := TfCategoria.Create(Application);
  try
    fCategoria.ShowModal;
  finally
    cbb3.Text := fCategoria.ds_grupoDESCCATEGORIA.AsString;
    fCategoria.Free;
    cbb3.SetFocus;
  end;
end;

procedure Tf_Planocontas.btn7Click(Sender: TObject);
 Var str_sql1: string;
begin
  inherited;
  if ds_desp.Active then
    ds_desp.Close;
  ds_desp.Params[0].AsString := cbb3.Text;
  ds_desp.Open;
  if DM.ds_1_planoc.Active then
    DM.ds_1_planoc.Close;
  if cbb3.Text <> '' then
  begin
    str_sql1 := 'Select * from PLANO_CONTA ';
    str_sql1 := str_sql1 + 'WHERE CONTAPAI = ';
    str_sql1 := str_sql1 + '''' + ds_despCONTA.AsString + '''';
    DM.ds_1_planoc.CommandText := str_sql1;
    DM.ds_1_planoc.Open;
  end
  else
  begin
    str_sql1 := 'Select * from PLANO_CONTA ';
    DM.ds_1_planoc.CommandText := str_sql1;
    DM.ds_1_planoc.Open;
  end;
end;

procedure Tf_Planocontas.FormCreate(Sender: TObject);
 Var str_sql: String;
begin
  //inherited;
  if DM.ds_1_planoc.Active then
    DM.ds_1_planoc.Close;
  str_sql := 'Select FIRST 500 * from PLANO_CONTA ';
  str_sql := str_sql + 'WHERE CONSOLIDA <> ''S'' order by NOME';
  DM.ds_1_planoc.CommandText := str_sql;
  DM.ds_1_planoc.Open;
  While not DM.ds_1_planoc.Eof do
  begin
    cbb1.Items.Add(DM.ds_1_planocNOME.AsString);
    DM.ds_1_planoc.Next;
  end;
  if DM.ds_1_planoc.Active then
    DM.ds_1_planoc.Close;
  str_sql := 'Select FIRST 500 * from PLANO_CONTA WHERE CONSOLIDA <> ''S'' order by CONTA';
  DM.ds_1_planoc.CommandText := str_sql;
  DM.ds_1_planoc.Open;
  if ds_desp.Active then
    ds_desp.Close;
  ds_desp.Params[0].AsString := 'GRUPO CMV - %';
  ds_desp.Open;
  While not ds_desp.Eof do
  begin
    cbb2.Items.Add(ds_despNOME.AsString);
    ds_desp.Next;
  end;
  ds_desp.Close;
end;

procedure Tf_Planocontas.FormShow(Sender: TObject);
begin
  sCtrlResize.CtrlResize(TForm(f_Planocontas));
end;

procedure Tf_Planocontas.btnIncluirClick(Sender: TObject);
var conta, pai, codreduz: string;
begin
  conta := DM.ds_1_planocCONTA.AsString;
  pai := DM.ds_1_planocCONTAPAI.AsString;
  codreduz := DM.ds_1_planocCODREDUZIDO.AsString;
  dsDtSrc.DataSet.Refresh;
  // incluir inherited;
  if not dsDtSrc.DataSet.Active then
     dsDtSrc.DataSet.open;
  dsDtSrc.DataSet.Append;

  DM.ds_1_planocCONTA.AsString := conta;
  DM.ds_1_planocCONTAPAI.AsString := pai;
  DM.ds_1_planocCODREDUZIDO.AsString := codreduz;
  dbcbbCONSOLIDA.Text := 'S';
  dbcbbRATEIO1.Text := 'N';
  dbedtCONTA.SetFocus;
end;

procedure Tf_Planocontas.dsDtSrcStateChange(Sender: TObject);
begin
  btnIncluir.Enabled  := dsDtSrc.State in [dsBrowse,dsInactive];
  btnGravar.Enabled   := dsDtSrc.State in [dsInsert,dsEdit];
  btnCancelar.Enabled := dsDtSrc.State in [dsInsert,dsEdit];
  btnExcluir.Enabled  := dsDtSrc.State in [dsBrowse];
  btnSair.Enabled     := dsDtSrc.State in [dsBrowse,dsInactive];

  if dsDtSrc.State in [dsInsert, dsEdit] then
  begin
    btnIncluir.Visible := False;
    btnexcluir.Visible := False;
    btngravar.Visible := True;
    btnGravar.Enabled := True;
    btnCancelar.Visible := True;
    btnCancelar.Enabled := True;    
  end;

  if dsDtSrc.State in [dsBrowse,dsInactive] then
  begin
    btnIncluir.Enabled := True;
    btnIncluir.Visible := True;
    btnexcluir.Visible := True;
    btnExcluir.Enabled := True;
    btngravar.Visible := False;
    btnCancelar.Visible := False;
  end;

end;

procedure Tf_Planocontas.btnGravarClick(Sender: TObject);
begin
  if DM.ds_1_planocCONTAPAI.AsString = '' then
    DM.ds_1_planocCONTAPAI.Clear;
  dm.ds_1_planocCONSOLIDA.AsString := dbcbbCONSOLIDA.Text;
  dm.ds_1_planocRATEIO.AsString    := dbcbbRATEIO1.Text;
  // Gravar inherited;

  try
    dsDtSrc.DataSet.Post;
    (dsDtSrc.DataSet as TClientDataset).ApplyUpdates(0);
  except
    on E : Exception do
    begin
      ShowMessage('Classe: ' + e.ClassName + chr(13) + 'Mensagem: ' + e.Message);
    end;
  end;


end;

procedure Tf_Planocontas.btnCancelarClick(Sender: TObject);
begin
  dsDtSrc.DataSet.Cancel;
end;

procedure Tf_Planocontas.btnExcluirClick(Sender: TObject);
begin
  if MessageDlg('Deseja realmente excluir este registro?',mtConfirmation, [mbYes,mbNo],0) = mrYes then
  begin
     dsDtSrc.DataSet.Delete;
     (dsDtSrc.DataSet as TClientDataSet).ApplyUpdates(0);
  end
  else
    Abort;
end;

procedure Tf_Planocontas.btnSairClick(Sender: TObject);
begin
  Close;
end;

end.
